#include "../include/thread_pool.h"
#include "../include/dbconn_pool.h"
#include "../include/func.h"


void *threadFunc(void *arg) {
    sharedRes_t *pSharedRes = (sharedRes_t*)arg;
    // 获得数据库连接
    MYSQL **p_mysql = (MYSQL**)malloc(sizeof(MYSQL*));
    // 加锁
    pthread_mutex_lock(&pSharedRes->mutex);
    int ret = get_dbconn(&pSharedRes->dbconnectPool, p_mysql);
    pthread_mutex_unlock(&pSharedRes->mutex);
    if (ret == -1) { // 连接已被用完（不会出现）
        return NULL;
    }
    while(1) {
        pthread_mutex_lock(&pSharedRes->mutex);
        while (pSharedRes->taskQueue.size == 0 && pSharedRes->exitFlag == 0) {
            pthread_cond_wait(&pSharedRes->cond, &pSharedRes->mutex);
        }
        //检查退出状态
        if(pSharedRes->exitFlag == 1){
            //工人因为要退出而被唤醒
            printf("worker going to exit!\n");
            pthread_mutex_unlock(&pSharedRes->mutex);
            pthread_exit(NULL);
        }
        //队列非空
        int fd_client = pSharedRes->taskQueue.pFront->fd_client;
        de_queue(&pSharedRes->taskQueue);
        pthread_mutex_unlock(&pSharedRes->mutex);
        printf("thread fd_client = %d\n", fd_client);
        printf("新客户端正在连接...\n");
        // 新会话
        int session_end = 0;
        userInfo_t user_info;
        bzero(&user_info, sizeof(userInfo_t));
        while (session_end == 0) {
            // 用户登录
            int login_success = 0;
            int account_op;
            int ret = recv(fd_client, &account_op, sizeof(int), 0);
            if (ret == 0) {
                // 客户端已退出
                printf("客户端已退出，会话结束\n");
                session_end = 1;
                break;
            }
            switch (account_op) {
            case TAGREGISTER:{
                                // 无论成功与否都继续响应菜单操作
                                user_register(fd_client, *p_mysql);
                                break;}
            case TAGLOGIN:{
                                // 登录成功进入网盘操作
                                ret = user_login(fd_client, *p_mysql, &user_info);
                                if (ret == 0) {
                                    login_success = 1;
                                }
                                break;}
            case TAGCANCEL:{
                                 // 无论成功与否都继续响应菜单操作
                                 user_cancel(fd_client, *p_mysql);
                                 break;}
            default:
                             break;
            }
            if (login_success == 0) {
                continue;
            }
            printf("当前目录:%s\n", user_info.current_path);
            // 网盘操作
            while (1) {
                int tag = -1;
                recv(fd_client, &tag, sizeof(int), 0);
                switch (tag) {
                case TAGPWD:
                    do_pwd(fd_client, user_info.current_path);
                    break;
                case TAGLS:
                    do_ls(fd_client, *p_mysql, user_info.current_path);
                    break;
                case TAGCD:
                    do_cd(fd_client, *p_mysql, user_info.current_path);
                    break;
                case TAGMKDIR:
                    do_mkdir(fd_client, *p_mysql, user_info.current_path);
                    break;
                case TAGRMDIR:
                    do_rmdir(fd_client, *p_mysql, user_info.current_path);
                    break;
                case TAGREMOVE:
                    do_remove(fd_client, *p_mysql, user_info.current_path);
                    break;
                case TAGPUTS:
                    do_puts(fd_client, *p_mysql, user_info.current_path);
                    break;
                case TAGGETS:
                    do_gets(fd_client, *p_mysql, user_info.current_path);
                    break;
                default:
                    break;
                }

            } 

        }
        if (session_end == 1) {
            close(fd_client);
        }
    }
}


int make_worker(sharedRes_t *pSharedRes, threadPool_t *pThreadPool) {
    for (int i = 0; i < pThreadPool->threadnum; i++) {
        pthread_t tid;
        pthread_create(&tid, NULL, threadFunc, pSharedRes);                                 
        pThreadPool->tidArr[i] = tid;
        printf("create tid %ld\n", tid);
    }   
    return 0;
}

int printQueue(taskQueue_t *pQueue) {
    node_t *pCurr = pQueue->pFront;
    while (pCurr != NULL) {
        printf("%3d ", pCurr->fd_client);
        pCurr = pCurr->pNext;
    }
    printf("\n");
    return 0;
}

